import os
from python_baway.common.xcommon import xshow_list, sep, xshow_table

print(os.name)
print(os.getcwd())
print(os.chdir('..'))
print(os.getcwd())
print(os.chdir('../../function'))
xpath = os.getcwd()
print(xpath)
xlist = os.listdir(xpath)
print(xlist)
# os.chdir('..')  # for test
xdirs, xfiles, xsize_map = [], [], {}
xdirs2, xdirs3 = [], []
for x in xlist:
    if not os.path.exists(x):
        continue
    if os.path.isdir(x):
        p = os.path.join(xpath, x)
        xdirs.append(p)
        xdirs2.append(p)
        xdirs3.append(x)
    elif os.path.isfile(x):
        p = xpath + os.sep + x
        xfiles.append(p)
        xsize_map[p] = os.path.getsize(p)
sep('dirs')
xshow_list(xdirs)
sep('files')
xshow_list(xfiles)
sep('sizes')
for p, xsize in xsize_map.items():
    print(f'{p} {xsize:,d}')
sep('sorted by size')
xarr = sorted([(p, xsize) for p, xsize in xsize_map.items()], key=lambda x: x[1])
xshow_table(xarr)
sep('try basename')
for idx in range(len(xdirs2)):
    xdirs2[idx] = xpath + os.sep + os.path.basename(xdirs2[idx])
print(xdirs == xdirs2)
print(f'__file__: {__file__} => {os.path.basename(__file__)}')
sep('try abspath')
for idx in range(len(xdirs3)):
    xdirs3[idx] = os.path.abspath(xdirs3[idx])
print(xdirs == xdirs3)

sep('split')
xfile = xarr[-1][0]


def xsplit(xfunc, xfile):
    head, tail = getattr(os.path, xfunc)(xfile)
    print(f'{xfunc}, |{head}|, |{tail}|')


xsplit('split', xfile)
xsplit('splitdrive', xfile)
xsplit('splitext', xfile)